layui.define(function (exports) {

    var LeftTabScroll = function () {
        this.default = {
            elem: null        //触发节点,
        }
    }

    LeftTabScroll.prototype.init = function (options) {

        $.extend(true, this.default, options);

        if (this.default.elem === null) {
            throwError('Donate Error:请配置触发节点参数elem!');
        }
        this.elAll = {
            main:$(this.default.elem).find(".tag_list_left"),
            mainUl:$(this.default.elem).find(".tag_list_left ul")
        }
        this.render();
        let MutationObserver = window.MutationObserver ||
            window.WebKitMutationObserver ||
            window.MozMutationObserver;

        let observer = new MutationObserver(function () {
            // console.log("observer", arguments);
            leftTabScroll.setTabHeight();
        })

        observer.observe(this.elAll.mainUl[0], {
            childList: true,
            attributes: false
        })
    }
    var upHtml = `
        <button class="up"><i class="zkuasgm-icon">&#xe632;</i></button>
    `
    var downHtml = `
        <button class="down"><i class="zkuasgm-icon">&#xe633;</i></button>
    `
    
    LeftTabScroll.prototype.render = function () {
        // console.log(33,this.elAll)
        var config = this.default;
        
        $(config.elem).prepend(upHtml)
        $(config.elem).append(downHtml)
        var mainUl = leftTabScroll.elAll.mainUl
        var that = this;
         // 绑定按钮事件
        $(config.elem).find(".up").on("click",function(){
            var caseGridHeight = $(".case_grid").height();
            var setHeight = parseInt($(window).height() / 100) * 100 - Math.ceil(caseGridHeight / 100) * 100
            var mainUlMt = mainUl.css("margin-top");
            var tagListSum = setHeight - mainUl.height();
            console.log(mainUlMt,tagListSum)
            if (mainUlMt != 0 + "px" && !mainUl.is(':animated')) {
                mainUl.animate({ marginTop: "+=100px" }, 300);
            }
            // that.checkBtn(this,mainUlMt,tagListSum);
        })
        $(config.elem).find(".down").on("click",function(){
            var caseGridHeight = $(".case_grid").height();
            var setHeight = parseInt($(window).height() / 100) * 100 - Math.ceil(caseGridHeight / 100) * 100
            var mainUlMt = mainUl.css("margin-top");
            var tagListSum = setHeight - mainUl.height();
            console.log(mainUlMt,tagListSum)
            if (mainUlMt != tagListSum + "px" && !mainUl.is(':animated')) {
                mainUl.animate({ marginTop: "-=100px" }, 300);
            }else{
                console.log(this)
            }
        })
        // li选中变底色
        $(config.elem).find("li").on("click",function(){
            if($(this).hasClass("hov")){
                $(this).removeClass("hov")
            }else{
                $(this).addClass("hov")
            }
        })
        this.setTabHeight();
    }
    LeftTabScroll.prototype.checkBtn = function(el,mt,sum){
        console.log(el,mt,sum)
        if (mt == 0 + "px") {
            $(el).css("display","none")
        }
    }
    LeftTabScroll.prototype.setTabHeight = function(){
        var config = this.default;
        var caseGridHeight = $(".case_grid").height();
        var setHeight = parseInt($(window).height() / 100) * 100 - Math.ceil(caseGridHeight / 100) * 100
        this.elAll.main.height(setHeight)
        var mainHeight= this.elAll.main.height()
        var mainUlHeight= this.elAll.main.find("ul").height()
        // 判断上下按钮的显示隐藏
        if (mainUlHeight > mainHeight) {
            $(config.elem).find("button").css("display", "block")
        } else {
            $(config.elem).find("button").css("display", "none")
        }
        // 重置margintop
        this.elAll.mainUl.animate({ marginTop: "0" }, 500);

    }
    LeftTabScroll.prototype.setTabHeight2 = function(){
        
    }
    // $(window).resize(function () {
    //     // console.log(1112,that,this)
    //     leftTabScroll.setTabHeight();
    //     // setTabHeight.call(leftTabScroll)
    //     // console.log(this)
    // })

    
    $("#testAdd").on("click",function(){
        $(".tag_list_bk ul").prepend(`
        <li><i class="zkuasgm-icon">&#xe614;</i>
										<!-- <span class="close" zkmap-event="clearSearch"><i class="zkuasgm-icon">&#xe629;</i></span> -->
										<p>test</p>
										<p class="num_line"><span class="layui-badge">345</span></p>
									</li>
        `)
        // leftTabScroll.setTabHeight();
    })
    $("#testPop").on("click",function(){
        $(".tag_list_bk ul li:first").remove();
        // leftTabScroll.setTabHeight();
    })
    
    /**
     * 抛出异常错误信息。
     * @param {String} msg
     */
    function throwError(msg) {
        if (layer && layer != undefined) {
            layer.msg(msg, { icon: 5 });
        }
        throw new Error(msg);
        return;
    }
    var leftTabScroll = new LeftTabScroll();
    exports('leftTabScroll', leftTabScroll);
});